/**
 * table懒加载封装
 */
import { get } from '@/utils'
export default <T = any>(api) => {
  const list = ref<T[]>([])
  const getList = (query: { level: number; parentId: any }, cb?): Promise<any[]> => {
    return new Promise((resolve, reject) => {
      get({ url: api, data: query }, ({ data, code }) => {
        data.map(v => {
          v.key = v.id
          v.isLeaf = !v.hasChildren
          if (v.hasChildren) {
            v.children?.map(child => {
              child.key = child.id
              child.isLeaf = !child.hasChildren
            })
          }
        })
        list.value = data
        resolve(data)
        cb?.(code == 200)
      })
    })
  }
  const loadMore = async (record, done) => {
    const list = await getList({
      level: record.level + 1,
      parentId: record.id,
    })
    list.map((v, i) => {
      v.key = v.id
      v.isLeaf = !v.hasChildren //最多3级
    })
    console.log(list)
    done(list)
  }
  return { getList, list, loadMore }
}
